class ::TitlesController < ApplicationController
  before_filter :require_user
  # GET /titles
  # GET /titles.xml
  def index
    authorize! :read, ::Title
    #@titles = ::Title.all
    #@titles = ::Title.paginate :page => params[:page]
    @search = ::Title.search(params[:search])
    @titles = @search.paginate(:per_page => 5,:page => params[:page])
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @titles }
    end
  end

  # GET /titles/1
  # GET /titles/1.xml
  def show
    authorize! :read , ::Title
    @title = ::Title.find(params[:id])
    @read_only = true
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @title }
    end
  end

  # GET /titles/new
  # GET /titles/new.xml
  def new
    authorize! :create , ::Title
    @title = ::Title.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @title }
    end
  end

  # GET /titles/1/edit
  def edit
    authorize! :update , ::Title
    @title = ::Title.find(params[:id])
    @read_only = false
  end

  # POST /titles
  # POST /titles.xml
  def create
    authorize! :create , ::Title
    @title = ::Title.new(params[:title])
    @title.user_log = current_user.login
    respond_to do |format|
      if @title.save
        format.html { redirect_to(@title, :notice => '::Title was successfully created.') }
        format.xml  { render :xml => @title, :status => :created, :location => @title }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @title.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /titles/1
  # PUT /titles/1.xml
  def update
    authorize! :update , ::Title
    @title = ::Title.find(params[:id])
    @title.user_log = current_user.login
    respond_to do |format|
      if @title.update_attributes(params[:title])
        format.html { redirect_to(@title, :notice => '::Title was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @title.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /titles/1
  # DELETE /titles/1.xml
  def destroy
    authorize! :destroy , ::Title
    @title = ::Title.find(params[:id])
    @title.user_log = current_user.login
    @title.destroy

    respond_to do |format|
      format.html { redirect_to(titles_url) }
      format.xml  { head :ok }
    end
  end
end
